OD for census tracts within each borough

Import required libraries

library(sf)
library(geojsonsf)
library(tidyverse)
library(tmap)

Import data from files - NYS OD

options(scipen = 999)
nyc_block_outlines <- geojson_sf('./data/nyc_block_outlines.geojson')
ny_origin_dest <- read_csv("./data/ny_od_main_JT00_2019.csv")

Define places of interest

boros <- c("Bronx", "Brooklyn", "Manhattan", "Queens")
county_codes <- c("005", "047", "061", "081")
boro_codes = c("2", "3", "1", "4")

Reduce borough outlines to Code and Geometry Map

nyc_block_outlines_map <- nyc_block_outlines %>%
  select(BCTCB2010, geometry)

Reduce NYS origin destination data to trips within borough of interest:

Utility functions;

get_boi_ods <- function(county_code){
  ny_origin_dest %>%
  filter(
    str_sub(as.character(w_geocode), 3, 5) == county_code &
      str_sub(as.character(h_geocode), 3, 5) == county_code
    )
}

boi_segment <- function(boro_ods, boro_code) {
  boro_ods %>%
  select(BCTCB2010, S000) %>%
  group_by(BCTCB2010) %>%
    summarize(
      S000 = sum(S000),
    ) %>%
  left_join(nyc_block_outlines_map) %>%
  st_as_sf() %>%
  st_make_valid()
}

get_boi_origin <- function(boro_ods, boro_code) {
  boro_ods %>%
    mutate(BCTCB2010 = str_c(boro_code, str_sub(as.character(h_geocode), 6, 15))) %>%
    boi_segment(boro_code)
}

get_boi_dest <- function(boro_ods, boro_code) {
  boro_ods %>%
    mutate(BCTCB2010 = str_c(boro_code, str_sub(as.character(w_geocode), 6, 15))) %>%
    boi_segment(boro_code) 
}
  1. The Bronx
bnx_ods <- get_boi_ods("005")
  1. Origins
bnx_origins <- bnx_ods %>%
  get_boi_origin(boro_code = "2")

Map the most popular origins

tm_shape(bnx_origins) +
  tm_polygons(
    col = "S000",
    style = "jenks",
    title = "Residents",
  ) + 
  tm_layout(
    title = "Most popular residential blocks in the Bronx",
    legend.outside = TRUE
  )

  1. Destinations
bnx_dests <- bnx_ods %>%
  get_boi_dest(boro_code = "2")

Map the most popular destinations

tm_shape(bnx_dests) +
  tm_polygons(
    col = "S000",
    style = "jenks",
    title = "Employers",
  ) + 
  tm_layout(
    title = "Most popular work blocks in the Bronx",
    legend.outside = TRUE
  )

  1. Brooklyn
bkn_ods <- get_boi_ods("047") 
  1. Origins
bkn_origins <- bkn_ods %>%
  get_boi_origin(boro_code = "3")

Map the most popular origins

tm_shape(bkn_origins) +
  tm_polygons(
    col = "S000",
    style = "jenks",
    title = "Residents",
  ) + 
  tm_layout(
    title = "Most popular residential blocks in Brooklyn",
    legend.outside = TRUE
  )

  1. Destinations
bkn_dests <- bkn_ods %>%
  get_boi_dest(boro_code = "3")

Map the most popular destinations

tm_shape(bkn_dests) +
  tm_polygons(
    col = "S000",
    style = "jenks",
    title = "Employers",
  ) + 
  tm_layout(
    title = "Most popular work blocks in Brooklyn",
    legend.outside = TRUE
  )

  1. Manhattan
mhn_ods <- get_boi_ods("061") 
  1. Origins
mhn_origins <- mhn_ods %>%
  get_boi_origin(boro_code = "1")

Map the most popular origins

tm_shape(mhn_origins) +
  tm_polygons(
    col = "S000",
    style = "jenks",
    title = "Residents",
  ) + 
  tm_layout(
    title = "Most popular residential blocks in Manhattan",
    legend.outside = TRUE
  )

  1. Destinations
mhn_dests <- mhn_ods %>%
  get_boi_dest(boro_code = "1")

Map the most popular destinations

mhn_dests_valid_dem <- mhn_dests %>%
  filter(!is.na(st_dimension(geometry)))
tm_shape(mhn_dests_valid_dem) +
  tm_polygons(
    col = "S000",
    style = "jenks",
    title = "Employers",
  ) + 
  tm_layout(
    title = "Most popular work blocks in Manhattan",
    legend.outside = TRUE
  )

  1. Queens
qns_ods <- get_boi_ods("081")
  1. Origins
qns_origins <- qns_ods %>%
  get_boi_origin(boro_code = "4")

Map the most popular origins

tm_shape(qns_origins) +
  tm_polygons(
    col = "S000",
    style = "jenks",
    title = "Residents",
  ) + 
  tm_layout(
    title = "Most popular residential blocks in Queens",
    legend.outside = TRUE
  )
## Warning: The shape qns_origins contains empty units.

  1. Destinations
qns_dests <- qns_ods %>%
  get_boi_dest(boro_code = "4")

Map the most popular destinations

qns_dests_valid_dem <- qns_dests %>%
  filter(!is.na(st_dimension(geometry)))
tm_shape(qns_dests_valid_dem) +
  tm_polygons(
    col = "S000",
    style = "jenks",
    title = "Employers",
  ) + 
  tm_layout(
    title = "Most popular work blocks in Queens",
    legend.outside = TRUE
  )